/**
 * Created by lihao on 2017/7/17.
 */

var arr = new Array();
var b_index = 0;
var b_last = 0;

var index = {
    banner: function() {
        arr = $('.banner_box ul li');
        $('body').everyTime('3s', function() {
            index.arrow_click(true);
        })
    },

    banner_hover: function() {
        $('.banner_box li').hover(
            function() {
                //index.scroll_end();
                $('body').stopTime();
            },
            function() {
                index.banner();
            }
        )
    },

    arrow_click: function(e) {
        b_last = b_index;
        if (e) {
            b_index++;
        } else {
            b_index--;
        }
        var lt = arr.get(b_last);
        $(lt).css({"visibility": "hidden"});
        if (b_index < arr.length && b_index >= 0) {
        } else {
            if (e) {
                b_index = 0;
                b_last = 0;
            } else {
                b_index = arr.length - 1;
                b_last = arr.length - 1;
            }
        }
        var it = arr.get(b_index);
        $(it).css({"visibility": "visible"});

    },

    nav: function(e) {
        $(window).scroll(function(){
            var myTop = $(window).scrollTop();
            if (myTop > $("#nav").offset().top) {
                if ($("#nav").hasClass("fixed_top_0") == false)
                $("#nav").addClass("fixed_top_0");
            } else if (myTop < e){
                $("#nav").removeClass("fixed_top_0");
            }
        })
        $(document).ready(function() {
            $("a.topLink").click(function() {
                $("html, body").animate({
                    scrollTop: $($(this).attr("href")).offset().top + "px"
                }, {
                    duration: 500,
                    easing: "swing"
                });
                return false;
            });
        });
    },

    video_hover: function() {
        $(".con_video div").hover(
            function() {
                $(".con_video .bg").addClass("index_video_animate");
            },
            function() {
                $(".con_video .bg").removeClass("index_video_animate");
            }
        );
    },

    skill_hover: function() {
        $(".con_ser_item img").hover(
            function() {
                $(this).addClass("index_skill_animate");
            },
            function() {
                $(this).removeClass("index_skill_animate");
            }
        );
    },

    title_hover: function() {
        $(".con_title").hover(
            function() {
                $(this).parent("a").siblings(".con_line").css({"width": "100px"});
            },
            function() {
                $(this).parent("a").siblings(".con_line").css({"width": "40px"});
            }
        );
    },

    year_hover: function() {
        $(".circle_text").hover(
            function() {
                $(this).addClass("index_skill_animate");
            },
            function() {
                $(this).removeClass("index_skill_animate");
            }
        );
    },

    about_hover: function() {
        $(".con_about_con").hover(
            function() {
                $(this).find(".con_about_div").css({"height": "100%"})
            },
            function() {
                $(this).find(".con_about_div").css({"height": "0"})
            }
        );
    },

    pro_hover: function() {
        $(".con_work_item").hover(
            function() {
                $(this).find("img").addClass("index_skill_animate");
            },
            function() {
                $(this).find("img").removeClass("index_skill_animate");
            }
        );
    },

    plate_hover: function() {
        $(".con_team_tool_plat").hover(
            function(){
                $(this).siblings("div").css({"visibility": "visible"});
            },
            function(){
                $(this).siblings("div").css({"visibility": "hidden"});
            }
        );
    }
}

var prolist = {
    item_hover: function() {
        $(".project_item").hover(
            function() {
                $(this).addClass("prolist_item_animate");
            },
            function() {
                $(this).removeClass("prolist_item_animate");
            }
        );
    }
}


//文章

var project = {
    //create
    create: function(ue, cover) {
        var title = $("input[name=title]").val();
        //var cover = cover;
        var content = ue.getContent();
        var desc = $("input[name=desc]").val();
        if (title.length == 0) {
            alert("文章标题不能为空");
            return false;
        }

        if (cover.length == 0) {
            alert("请添加封面");
            return false;
        }

        if (desc.length == 0) {
            alert("描述不能为空")
            return false;
        }

        if (content.length == 0) {
            alert("请输入文章内容")
            return false;
        }
        alert(content + '\n' + title + '\n' + cover + '\n' + desc);

        request('post', '../project/create', {
            'content':content,
            'title':title,
            'desc':desc,
            'cover':cover,
            'typeid':project.typeindex
        }, function(content) {
            alert(content['msg']);
            window.location.href = "../project/prolist"
        })
    },
    edit: function(ue, cover, proid) {
        var title = $("input[name=title]").val();
        var content = ue.getContent();
        var desc = $("input[name=desc]").val();
        if (title.length == 0) {
            alert("文章标题不能为空");
            return false;
        }

        if (cover.length == 0) {
            alert("请添加封面");
            return false;
        }

        if (desc.length == 0) {
            alert("描述不能为空")
            return false;
        }

        if (content.length == 0) {
            alert("请输入文章内容")
            return false;
        }
        request('post', '../editpro', {
            'proid': proid,
            'content':content,
            'title':title,
            'desc':desc,
            'cover':cover,
            'typeid':project.typeindex
        }, function(content) {
            alert(content['msg']);
            window.location.href = "../prolist"
        })
    },
    typeindex: 1,
    typeclick: function() {
        $('.title_left li').click(function() {
            project.typeindex = $(this).val();
            $(this).removeClass('bg_f0');
            $(this).addClass('bg_e0');
            $(this).siblings('li').each(function() {
                $(this).removeClass('bg_e0');
                $(this).addClass('bg_f0');
            })
        })
    },
    cateclick: function() {
        $('.list_cate li').click(function() {
            window.location.href = '/project/prolist/' + $(this).val();
        })
    }
}

function request(method, url, data, func) {
    if (method == "get") {
        $.get(url, data, function(text, state) {
            if (state == "success") {
                if (text['success'] == 1) {
                    func(text['content'])
                } else {
                    alert("请求失败: " + text['content']['msg']);
                }
            } else {
                alert("网络错误");
            }
        }, 'json')
    } else {
        $.post(url, data, function(text, state) {
            alert(state);
            if (state == "success") {
                if (text['success'] == 1) {
                    func(text['content'])
                } else {
                    alert("请求失败: " + text['content']['msg']);
                }
            } else {
                alert("网络错误");
            }
        }, 'json')
    }
}

var photo = {
    create_dir: function() {
        dir = $("input[name=dirname]").val();
        desc = $("textarea[name=dirdesc]").val();
        if (dir.length == 0) {
            alert("文件夹名称不能为空!");
            return false;
        }
        request("post", "../photo/createdir", {
            'dir':dir,
            'desc':desc
        }, function(contnet) {
            alert(content['msg']);
            //photo.hover_hide();
            location.reload();
        })
    },
    hover_hide: function() {
        $('.create_hover').css({"visibility": "hidden"})
    },
    hover_show: function() {
        $('.create_hover').css({"visibility": "visible"})
    },
}


//用户相关

var account = {
    register: function() {
        var user = $("input[name=username]").val();
        var pw = $("input[name=password]").val();
        if (user.length == 0) {
            alert("请输入邮箱")
            return false;
        }
        if (pw.length == 0) {
            alert("请输入密码")
            return false;
        }

        request('post', '../api/register', {
            'user': user,
            'pw':pw
        }, function(content) {
            alert(content['msg']);
            window.location.href = '../account';
        })
    },
    login: function() {
        var user = $("input[name=username]").val();
        var pw = $("input[name=password]").val();
        if (user.length == 0) {
            alert("请输入邮箱")
            return false;
        }
        if (pw.length == 0) {
            alert("请输入密码")
            return false;
        }

        request('post', '../api/login', {
            'user': user,
            'pw': pw
        }, function(content) {
            window.location.href = '../home';
        })
    },
    logout: function() {
        request('get', '../api/logout',null, function(content) {
            window.location.href = '../home';
        })
    }
}